/**
 * Created by hsia on 2017/12/26.
 */
var business=[],carBrand=[],carType=[];
var CimcVeHicle = function (targetObj) {
	this.targetObj = targetObj;
	this.sysUtils = new kendoUtils();
	this.init();
	return this;
}
CimcVeHicle.prototype = {
		init: function () {
			var that = this;
			var fieldArray = this.sysUtils.getBindFieldArray(that.targetObj.find('.search-panel'));
			var functionArray = [
			                     {fName: 'search_btn', fs: this.findData},
			                     {fName: 'reset_btn', fs: this.resetInfo}
			                     ];
			this.models = new bindBaseModel(null, fieldArray, functionArray, null);
			this.sysUtils.bindDoucment(this.models, this.targetObj, this);
			var sHeight = (this.targetObj.find('.search-panel')[0].clientHeight);
			var allHeight = (this.targetObj[0].clientHeight);
			this.targetObj.find('.dataList').height(allHeight-sHeight-2);
			this.initSelect();
			this.initTable();
			this.findData();
		},
		initSelect:function(){
			var that = this;

			JsonpAjax(baseApiPath+"cimc/business.eif",null,"json",function(result){
				if(result.resCode==1){
					business = result.data;
					cimcVeHicle['business']=business;
					that.createBusinessDropDownList({div:that.targetObj.find("#company"),select:business,unshift:true},that);
				}
			},true);
			JsonpAjax(baseApiPath+"cimc/carBrand.eif",null,"json",function(result){
				if(result.resCode==1){
					carBrand = result.data;
					cimcVeHicle['carBrand']=business;
					that.createCarBrandDropDownList({div:that.targetObj.find("#carBrand"),select:carBrand,unshift:true},that);
				}
			},true);
			JsonpAjax(baseApiPath+"cimc/carType.eif",null,"json",function(result){
				if(result.resCode==1){
					carType = result.data;
					cimcVeHicle['carType']=business;
					that.createCarTypeDropDownList({div:that.targetObj.find("#carType"),select:carType,unshift:true},that);
				}
			},true);

			var inoutType =[];
			inoutType.push({name:'入园',code:0});
			inoutType.push({name:'出园',code:1});
			Utils.createAjaxDropDownList({div:that.targetObj.find("#inoutType"),select:inoutType,unshift:true},that)
		},
		/*
		 * 重置按钮
		 */
		resetInfo:function(e){
			var that = $(e.target).data("that");
			that.sysUtils.setBindFieldNull(that.models.bindFieldArray);
		},
		initTable : function() {
			var that = this;
			var columns = [
			               {
			            	   width : 30,
			            	   headerTemplate : '<input type="checkbox" gridId="cimcVehiclelist" onclick="gridCheckAll(this)" />',
			            	   template : '<input type="checkbox" name="cimcVehicle_template_id" value="${rfid}" />'
			               },
			               {
			            	   title : '商户',
			            	   field : 'companyName'
			               },
			               {
			            	   title : '车辆品牌',
			            	   field : 'vehiclebrandName'
			               },
			               {
			            	   title : '车辆类型',
			            	   field : 'vehicletypeName'
			               },
			               {
			            	   title : '车架号',
			            	   field : 'vehicleframe'
			               },

			               {
			            	   width : 100,
			            	   title : '状态',
			            	   field : 'outerflag == 0 ? "入场":outerflag == 1?"出场":"调拨"'
			               },
			               {
			            	   width : 170,
			            	   title : '进场时间',
			            	   field : 'indate'
			               },
			               {
			            	   width : 170,
			            	   title : '出场时间',
			            	   field : 'outdate'
			               }
			               ];
			that.sysUtils.createGird(that.targetObj.find('#cimcVehiclelist'), {
				selectable : 'single',
				autoSync : true,
				columnMenu : true,
				pageable : {input : true, numeric : false},
				columns : columns,
				change : function(e) {
					var selected = $.map(this.select(), function(itm) {
						return $(itm).find('input[type=checkbox]').val();
					});
					that.selectRowId = selected[0];
				},
				dataBound:function(){
					that.targetObj.find("#cimcVehiclelist").find("tr:gt(0)").unbind("dblclick");
					that.targetObj.find("#cimcVehiclelist").find("tr:gt(0)").dblclick(function(e){
						var grid = that.targetObj.find("#cimcVehiclelist").data("kendoGrid");
						var dataItem = grid.dataItem(grid.select());
						//打开查看窗口
						that.openvVehicleWindow(dataItem["rfid"]);
					});
				}
			});
		},
		findData : function (e) {
			var that = this;

			if (!isEmpty(e)) {
				that = $(e.target).data('that');
			}
			var param = that.getQueryParams();
			if(null == param) return;
			var dataT = {jsonParam : JSON.stringify(param)};
			var dataSource = that.sysUtils.getDataSource({url : baseApiPath + 'cimc/vehicles', data : dataT, dataType : 'odata'});
			that.targetObj.find('#cimcVehiclelist').data('kendoGrid').setDataSource(dataSource);

		},
		getQueryParams:function(){
			 if(validityForm(this.targetObj)){
		            var param = this.sysUtils.getBindDocJson(this.models.bindFieldArray);
		            //转换时间格式
		            if(isNotEmpt(param["beginTime"])){
		                param["beginTime"] = kendo.toString(param["beginTime"],'yyyy-MM-dd HH:mm:ss');
		            }
		            if(isNotEmpt(param["endTime"])){
		                param["endTime"] = kendo.toString(param["endTime"],'yyyy-MM-dd HH:mm:ss');
		                param["endTime"] = param["endTime"].split(' ')[0]+' 23:59:59';
		            }
		            return param;
		        }
			 return null;
		},
		openvVehicleWindow:function(vehicleid,isEdit){
			var that = this;
			var titles = "查看车辆详情";
			that.editResult = null;
			that.isEdit = isEdit;
			var openwidonw = function(){
				that.editDiv = that.sysUtils.openWindow(that.editDiv,{
					title:titles,
					height:"680px",
					width:"945px",
					resizable:false,
					content:"views/parkLotCenter/managerCenter/cimcVehicleEdit.html"
				}, true);
			};
			var ajaxPost = function(){
				JsonpAjax(baseApiPath+"cimc/" + vehicleid,null,"json",function(result){
					that.editResult = result;

				});
				openwidonw();
			};
			ajaxPost();
		},
		createBusinessDropDownList:function (json,that,callback) {
			var dataSource = json["select"];
			if(json["unshift"]){
				dataSource.unshift({companyname:"--请选择--",companyid:""});
			}
			var dropDownList = json["div"].kendoDropDownList({
				dataTextField: "companyname",
				dataValueField: "companyid",
				dataSource: dataSource
			}).data("kendoDropDownList");
			if(null != json["dataBound"] && typeof json["dataBound"] =="function"){
				dropDownList.bind("dataBound",json["dataBound"]);
			}
			if(null != json["change"] && typeof json["change"] =="function"){
				dropDownList.bind("change",json["change"]);
			}
			//回调最外层函数
			if(null != callback &&  typeof callback =="function"){
				callback.apply(that);
			}
		},
		createCarBrandDropDownList:function (json,that,callback) {
			var dataSource = json["select"];
			if(json["unshift"]){
				dataSource.unshift({vehiclebrandname:"--请选择--",vehiclebrandid:""});
			}
			var dropDownList = json["div"].kendoDropDownList({
				dataTextField: "vehiclebrandname",
				dataValueField: "vehiclebrandid",
				dataSource: dataSource
			}).data("kendoDropDownList");
			if(null != json["dataBound"] && typeof json["dataBound"] =="function"){
				dropDownList.bind("dataBound",json["dataBound"]);
			}
			if(null != json["change"] && typeof json["change"] =="function"){
				dropDownList.bind("change",json["change"]);
			}
			//回调最外层函数
			if(null != callback &&  typeof callback =="function"){
				callback.apply(that);
			}
		},
		createCarTypeDropDownList:function (json,that,callback) {
			var dataSource = json["select"];
			if(json["unshift"]){
				dataSource.unshift({vehicletypename:"--请选择--",vehicletypeid:""});
			}
			var dropDownList = json["div"].kendoDropDownList({
				dataTextField: "vehicletypename",
				dataValueField: "vehicletypeid",
				dataSource: dataSource
			}).data("kendoDropDownList");
			if(null != json["dataBound"] && typeof json["dataBound"] =="function"){
				dropDownList.bind("dataBound",json["dataBound"]);
			}
			if(null != json["change"] && typeof json["change"] =="function"){
				dropDownList.bind("change",json["change"]);
			}
			//回调最外层函数
			if(null != callback &&  typeof callback =="function"){
				callback.apply(that);
			}
		},
		/**
		 * 导出Excel文件
		 */
		exportExcel:function(){
			var params = this.getQueryParams();
			if(null == params) return;
			window.open(baseApiPath+'cimc/exportCimcVehicles?jsonData='+encodeURI(JSON.stringify(params)));
		}

}

var CimcVehicleEdit = function (targetObj) {
	this.targetObj = targetObj;
	this.sysUtils = new kendoUtils();
	this.init();
	return this;
}
CimcVehicleEdit.prototype = {
		init: function () {
			var fieldArray = this.sysUtils.getBindFieldArray(this.targetObj);
			var functionArray = [];
			this.models = new bindBaseModel(null, fieldArray, functionArray, null);
			this.sysUtils.bindDoucment(this.models, this.targetObj, this);
			this.loadInfo();
		},
		loadInfo:function () {
			var that = this;
			var editResult = cimcVeHicle.editResult;
			businessLable:for(var i=0;i<business.length;i++){
				if(business[i].companyid==editResult.companyid){
					editResult.companyName=business[i].companyname;
					break businessLable;
				}
			}
			carBrandLabel:for(var i=0;i<carBrand.length;i++){
				if(carBrand[i].vehiclebrandid==editResult.vehiclebrandid){
					editResult.vehiclebrandName=carBrand[i].vehiclebrandname;
					break carBrandLabel;
				}
			}
			carTypeLabel:for(var i=0;i<carType.length;i++){
				if(carType[i].vehicletypeid==editResult.vehicletypeid){
					editResult.vehicletypeName=carType[i].vehicletypename;
					break carTypeLabel;
				}
			}
			that.sysUtils.setVariantBindFieldValue(editResult);
		}
}
